package com.xxxx.server.controller;


import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.po.EmployeeVo;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.IEmployeeService;
import com.xxxx.server.utils.EasyPoiUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 */
@Api("基本资料")
@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {

    // 使workId字段自动增加
    private static Integer WORK_ID_AUTO = 10000101;

    @Autowired
    private IEmployeeService iEmployeeService;
    @Resource
    private EmployeeMapper employeeMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ApiOperation(value = "进入查询用户列表功能")
    @GetMapping()
    public Map<String,Object> selectAllList(EmployeeVo vo){
        Map<String,Object> map=iEmployeeService.selectAllList(vo);
        return map;
    }

    @ApiOperation(value = "单个删除用户功能")
    @DeleteMapping("/{id}")
    public RespBean deleteUser(@PathVariable Integer id){
        iEmployeeService.deleteUser(id);
        RespBean respBean=new RespBean();
        return respBean.success("删除成功！");
    }

    @ApiOperation(value = "添加员工")
    @PostMapping("/")
    public RespBean addUser(@RequestBody Employee employee){
        return iEmployeeService.insertEmployee(employee);
    }


    @ApiOperation(value = "查询工号")
    @GetMapping("/maxWorkID")
    public Map<String,Integer> selectWorkId(){
        List<Employee> list=iEmployeeService.selectWorkId();

        Map<String,Integer> map=new HashMap<>();
        Integer i=1;
        map.put("obj",WORK_ID_AUTO++);
        return map;
    }

    @ApiOperation(value = "查询职称")
    @GetMapping("/joblevels")
    public List<Joblevel> selectJobLevels(){
        return iEmployeeService.selectJobLevels();
    }

    @ApiOperation(value = "查询民族")
    @GetMapping("/nations")
    public List<Nation> selectNation(){
        return iEmployeeService.selectNation();
    }

    @ApiOperation(value = "查询政治面貌")
    @GetMapping("/politicsstatus")
    public List<PoliticsStatus> selectPoliticsStatus(){
        return iEmployeeService.selectPoliticsStatus();
    }

    @ApiOperation(value = "查询所属部门")
    @GetMapping("/deps")
    public List<Department> selectDepartment(){
        return iEmployeeService.selectDepartment();
    }

    @ApiOperation(value = "查询职位")
    @GetMapping("/positions")
    public List<Position> selectPosition(){
        return iEmployeeService.selectPosition();
    }


    @ApiOperation(value = "文件导入")
    @PostMapping("/import")
    public void importFile(@RequestParam("file") MultipartFile file, HttpSession session) throws Exception {
        ImportParams params = new ImportParams();
        //设置标题行
        params.setTitleRows(1);
        //设置表头行
        params.setHeadRows(1);
        //导入工具类，得到的数据放入list集合中
        List<Employee> list = EasyPoiUtils.importExcel(file.getInputStream(), Employee.class, params);
        //将数据批量插入数据库
        iEmployeeService.saveBatch(list);
    }

    @ApiOperation(value = "文件导出")
    @GetMapping("/export")
    public void exportFile(HttpServletRequest request,HttpServletResponse response){
        //导出Excel
        EasyPoiUtils.exportExcel(iEmployeeService.selectAllEmployee(),"员工基本资料信息表","员工信息表",Employee.class,"员工信息表.xls",response);
    }


    @ApiOperation(value = "更新员工资料")
    @PutMapping("/")
    public RespBean updateEmployee(@RequestBody Employee employee){
        return iEmployeeService.updateById(employee) ? RespBean.success("操作成功") : RespBean.error("操作失败");
    }


}
